package com.five.community.controller;

import com.five.community.bean.*;
import com.five.community.bean.vo.BuildingVo;
import com.five.community.bean.vo.ComplaintVo;
import com.five.community.bean.vo.HousingVo;
import com.five.community.bean.vo.PersonVo;
import com.five.community.contant.AppContants;
import com.five.community.service.BuildingService;
import com.five.community.service.HousingService;
import com.five.community.service.PersonService;
import com.five.community.service.VillageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/housing")
public class HousingController {

    @Autowired
    private VillageService villageService;

    @Autowired
    private BuildingService buildingService;

    @Autowired
    private HousingService housingService;
    @Autowired
    private PersonService personService;

    //房产管理一览页面
    @RequestMapping("/listUi")
    public String listUi(ModelMap map, Integer pageNum){
        if (pageNum == null) {
            pageNum = 1;
        }
        try {
            PageHelper.startPage(pageNum,AppContants.pageSize);
           List<HousingVo> housingVos = housingService.selectAllF();
           PageInfo<HousingVo> pageInfo = new PageInfo<>(housingVos);
           map.addAttribute("pageInfo", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "housing";
    }
    //房产搜索根据名字
    @RequestMapping("/selectUi")
    public String selectUi(ModelMap map,String property,Integer pageNum){
        if (property=="") {
            return "redirect:/housing/listUi";
        } else {
            try {
                if (pageNum == null) {
                    pageNum = 1;
                }
                PageHelper.startPage(pageNum,AppContants.pageSize);
                List<HousingVo> housingVos = housingService.selectByName(property);
                PageInfo<HousingVo> pageInfo = new PageInfo<>(housingVos);
                map.addAttribute("pageInfo", pageInfo);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "housing";
    }
    //房产信息删除
    @RequestMapping("/delete")
    public String delete(long id){
        try {
            housingService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/housing/listUi";
    }
    //添加页面
    @RequestMapping("/addUi")
    public String addUi(ModelMap map) {
        try {
            List<Village> villages = villageService.selectAll();
            map.addAttribute("villages", villages);
            List<BuildingVo> buildings = buildingService.selectAll();
            map.addAttribute("buildings",buildings);
            List<PersonVo> persons = personService.selectAllPerson();
            map.addAttribute("persons",persons);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "housing_add";
    }
    //添加房产
    @RequestMapping("/add")
    public String add(HousingVo housingVo){
        try {
            long vid = Long.parseLong(housingVo.getComName());
            long bid = Long.parseLong(housingVo.getBuildName());
            Housing housing = new Housing(vid,bid,housingVo.getHouseId(),housingVo.getProperty(),housingVo.getPerName(),housingVo.getContactWay(),housingVo.getRoomNumber(),housingVo.getUnit(),housingVo.getFloor(),housingVo.getDescribes(),new Date());
            housingService.insert(housing);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/housing/addUi";
    }
    //修改编辑页面
    @RequestMapping("/editUi")
    public String editUi(long id, ModelMap map){
        try {
            List<Village> villages = villageService.selectAll();
            map.addAttribute("villages", villages);
            List<BuildingVo> buildings = buildingService.selectAll();
            map.addAttribute("buildings",buildings);
            Housing housing = housingService.selectById(id);
            map.addAttribute("housing",housing);
            List<PersonVo> persons = personService.selectAllPerson();
            map.addAttribute("persons",persons);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "housing_edit";
    }
    //修改编辑房产
    @RequestMapping("/edit")
    public String edit(HousingVo housingVo){
        try {
            long vid = Long.parseLong(housingVo.getComName());
            long bid = Long.parseLong(housingVo.getBuildName());
            Housing housing = new Housing(housingVo.getId(),vid,bid,housingVo.getHouseId(),housingVo.getProperty(),housingVo.getPerName(),housingVo.getContactWay(),housingVo.getRoomNumber(),housingVo.getUnit(),housingVo.getFloor(),housingVo.getDescribes(),housingVo.getCheckIn());
            housingService.update(housing);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "housing_edit";
    }

}
